package com.yida.gwj.expend.service.impl;

import com.yida.common.core.constant.SecurityConstants;
import com.yida.common.core.domain.R;
import com.yida.common.core.utils.StringUtils;
import com.yida.gwj.api.domain.dto.GwjExpendHouseAndOwnerDTO;
import com.yida.gwj.api.domain.dto.GwjGVoteResultInnerDTO;
import com.yida.gwj.api.domain.request.GwjVoteResultInnerRequest;
import com.yida.gwj.api.service.RemoteGwjVoteService;
import com.yida.gwj.expend.mapper.GwjExpendHouseAndOwnerMapper;
import com.yida.gwj.expend.service.IGwjExpendBaseHouseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hmh
 * @date 2023/06/17
 */
@Service
public class GwjExpendBaseHouseServiceImpl implements IGwjExpendBaseHouseService {

    private static final Logger log = LoggerFactory.getLogger(GwjExpendBaseHouseServiceImpl.class);

    @Autowired
    private GwjExpendHouseAndOwnerMapper gwjExpendHouseAndOwnerMapper;

    @Autowired
    private RemoteGwjVoteService remoteGwjVoteService;

    /*
     * 查询房屋业主投票列表
     */
    @Override
    public List<GwjExpendHouseAndOwnerDTO> selectHouseAndOwnerVoteInfosByBizIdAndBizNum(Long bizId, Long bizNum) {
        // PageUtils.startPage();
        List<GwjExpendHouseAndOwnerDTO> list = gwjExpendHouseAndOwnerMapper.selectGwjExpendHouseAndOwnerList(bizId);
        if (StringUtils.isEmpty(list)) {
            return new ArrayList<GwjExpendHouseAndOwnerDTO>();
        }
        List<String> cardNumArray = new ArrayList<String>();
        list.stream().forEach(gehao -> {
            String cardNum = gehao.getCardNum();
            if (StringUtils.isNotEmpty(cardNum)) {
                cardNumArray.add(cardNum);
            }
        });
        // 获取公维金投票结果
        Map<String, String> gVoteResultMap = getVoteInfosByBizNum(bizNum, cardNumArray);
        String voteDefault = "未选择";
        list.stream().forEach(gehao -> {
            String cardNum = gehao.getCardNum();
            System.out.println("cardNum:" + cardNum);
            // 优先判断身份证内是否有投票信息
            String cardNumVoteResult = gVoteResultMap.get(cardNum);
            // 身份证不为空，获取对应投票结果，如果投票结果为空则赋默认值;身份证号为空则赋默认值
            String voteResult = StringUtils.isNotEmpty(cardNum) ? (StringUtils.isNotEmpty(cardNumVoteResult) ? cardNumVoteResult : voteDefault) : voteDefault;
            if (StringUtils.isNotEmpty(voteResult) && !voteDefault.equals(voteResult)) {
                gehao.setOwnerOpinion(voteResult);
                return;
            }
            // 再度判断手机号内是否有投票信息
            String phonenumber = gehao.getPhonemumber();
            System.out.println("phonenumber:" + phonenumber);
            // 手机号不为空，获取对应投票结果，如果投票结果为空则赋默认值;手机号空则赋默认值
            String phonenumberVoteResult = gVoteResultMap.get(phonenumber);
            voteResult = StringUtils.isNotEmpty(phonenumber) ? (StringUtils.isNotEmpty(phonenumberVoteResult) ? phonenumberVoteResult : voteDefault) : voteDefault;
            gehao.setOwnerOpinion(voteResult);
        });
        return list;
    }

    private Map<String, String> getVoteInfosByBizNum(Long bizNum, List<String> cardNumArray) {
        Map<String, String> resultMap = new HashMap<String, String>();
        // 获取投票结果
        GwjVoteResultInnerRequest request = new GwjVoteResultInnerRequest();
        request.setBizNum(bizNum);
        request.setOwnerCards(cardNumArray);
        R<List<GwjGVoteResultInnerDTO>> result = remoteGwjVoteService.innerVoteResult(request, SecurityConstants.INNER);
        if (R.FAIL == result.getCode()) {
            log.error(result.getMsg());
            // throw new ServiceException("未获取到业务编号" + bizNum + "的投票结果！请核对投票信息！");
            return resultMap;
        }
        List<GwjGVoteResultInnerDTO> list = result.getData();
        list.stream().forEach(gvri -> {
            String option = gvri.getOptionName();
            resultMap.put(gvri.getOwnerPhone(), option);
            resultMap.put(gvri.getOwnerCard(), option);
        });
        return resultMap;
    }
}
